package sg.edu.nus.iss.billing.gui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileInputStream;
import java.util.Properties;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.SwingConstants;

import sg.edu.nus.iss.billing.BillingApplication;
import sg.edu.nus.iss.billing.BillingConstants;
import sg.edu.nus.iss.billing.BillingConstants.SystemState;
import sg.edu.nus.iss.billing.controller.BillController;
import sg.edu.nus.iss.billing.model.account.Account;

/**
 * @author Yeo Hee Ling, Ananda, Saravana
 *
 */
public class BillPanel extends JPanel implements ActionListener {
	
	private static final long serialVersionUID = 1L;
	
	// Declaration of Global Variables.
	private Properties properties;
	private BillingApplication billingAppl;
	private JButton btnSearchResult, btnGenBill;
	private JPanel searchResultPanel;
	private JTextPane billContentPane;
	private JLabel lblGenMth, lblGenYear;
	private JComboBox cbGenMth, cbGenYear;
	private String[] years = {"2011", "2012", "2013", "2014", "2015"};
	private String[] months = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
	private String[] monthsIndex = {"01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12"};
	private BillController billController = new BillController();
	private Account account;
	private String period;
	
	// Class Constructor.
	public BillPanel(BillingApplication billingAppl, SystemState selectedMode, JPanel searchResultPanel, Account account, String period) {
		this.billingAppl = billingAppl;
		this.searchResultPanel = searchResultPanel;
		this.account = account;
		this.period = period;
		this.setOpaque(false);
		
		loadConfiguration();
		
		if(SystemState.BILL_VIEW == selectedMode)
			add(createViewBillPanel());
		else if(SystemState.BILL_GENERATE_MTHLY == selectedMode)
			add(createGenerateBillPanel());
	}
	
	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == btnSearchResult) {
			billingAppl.displayPage(searchResultPanel);
		}
		else if(e.getSource() == btnGenBill) {
			String month = monthsIndex[cbGenMth.getSelectedIndex()];
			String year = cbGenYear.getSelectedItem().toString();
			String msg  = billController.generateBill(year+month);
			JOptionPane.showMessageDialog(null, msg, "Generate Customer Bill", JOptionPane.INFORMATION_MESSAGE);
			
		}
	}
	
	private void loadConfiguration() {
		try {
			properties = new Properties();
			FileInputStream in = new FileInputStream(BillingConstants.PROPERTIES_BILL);
			properties.load(in);
			in.close();
		}
		catch(Exception ex) {
			ex.printStackTrace();
		}
	}
	
	private JPanel createViewBillPanel() {
		// 1. To determine the title of the Bill panel.
		String billPanelTitle = properties.getProperty("title.viewCustomerBill");
		
		// 2. Construct Bill Details Panel.
		JPanel billContentPanel = new JPanel();
		billContentPanel.setOpaque(false);
		billContentPanel.setLayout(new BorderLayout());
		
		billContentPane = new JTextPane();
		billContentPane.setEditable(false);
		billContentPane.setContentType("text/html");
		billContentPane.setText(billController.viewBill(account, period));
		
		JScrollPane billContentScrollPane = new JScrollPane();
		billContentScrollPane.setViewportView(billContentPane);
		billContentScrollPane.setPreferredSize(new Dimension(800, 550));
		billContentPanel.add(billContentScrollPane, BorderLayout.NORTH);
		
		JPanel buttonPanel = new JPanel();
		buttonPanel.setOpaque(false);
		buttonPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
		
		btnSearchResult = new JButton(properties.getProperty("label.bill.button.searchResult"));
		btnSearchResult.setPreferredSize(new Dimension(175, 25));
		btnSearchResult.addActionListener(this);
		buttonPanel.add(btnSearchResult);
		billContentPanel.add(buttonPanel, BorderLayout.SOUTH);
		
		// 3. Construct Header Panel
		JPanel headerPanel = new JPanel();
		headerPanel.setOpaque(false);
		headerPanel.setLayout(new GridLayout(0, 1, 10, 10));
		
		JLabel msgLabel = new JLabel(billPanelTitle);
		msgLabel.setFont(new Font("Tahoma", Font.BOLD, 18));
		msgLabel.setHorizontalAlignment(SwingConstants.CENTER);
		headerPanel.add(msgLabel);
		
		ImageIcon imgDivider = new ImageIcon("./images/divider.jpg");
		JLabel lblDivider = new JLabel(imgDivider);
		headerPanel.add(lblDivider);
		
		JPanel viewBillPanel = new JPanel();
		viewBillPanel.setOpaque(false);
		viewBillPanel.setLayout(new BorderLayout());
		viewBillPanel.add(headerPanel, BorderLayout.NORTH);
		viewBillPanel.add(billContentPanel, BorderLayout.CENTER);
		
		return viewBillPanel;
	}
	
	private JPanel createGenerateBillPanel() {
		// 1. To determine the title of the Bill panel.
		String billPanelTitle = properties.getProperty("title.GenerateMthlyBill");
		
		// 2. Construct generate Bill Details Panel.
		JPanel genBillCriteriaPanel = new JPanel();
		GridBagLayout layout = new GridBagLayout();
		genBillCriteriaPanel.setLayout(layout);
		genBillCriteriaPanel.setOpaque(false);
		
		GridBagConstraints constraints1 = new GridBagConstraints();
		constraints1.insets = new Insets(5, 5, 5, 5);
		constraints1.fill = GridBagConstraints.HORIZONTAL;
		
		GridBagConstraints constraints2 = new GridBagConstraints();
		constraints2.insets = new Insets(5, 5, 5, 50);
		constraints2.fill = GridBagConstraints.HORIZONTAL;
		
		GridBagConstraints constraints3 = new GridBagConstraints();
		constraints3.insets = new Insets(20, 5, 20, 5);
		constraints3.gridwidth = 4;
		constraints3.gridy = 3;
		constraints3.anchor = GridBagConstraints.CENTER;
		
		constraints1.gridx = 0;
		constraints1.gridy = 0;
		lblGenMth = new JLabel(properties.getProperty("label.bill.generatingMth"));
		lblGenMth.setHorizontalAlignment(JLabel.RIGHT);
		genBillCriteriaPanel.add(lblGenMth, constraints1);
		
		constraints2.gridx = 1;
		constraints2.gridy = 0;
		cbGenMth = new JComboBox(months);
		cbGenMth.setPreferredSize(new Dimension(175, 25));
		genBillCriteriaPanel.add(cbGenMth, constraints2);
		
		constraints1.gridx = 2;
		constraints1.gridy = 0;
		lblGenYear = new JLabel(properties.getProperty("label.bill.generatingYear"));
		lblGenYear.setHorizontalAlignment(JLabel.RIGHT);
		genBillCriteriaPanel.add(lblGenYear, constraints1);
		
		constraints2.gridx = 3;
		constraints2.gridy = 0;
		cbGenYear = new JComboBox(years);
		cbGenYear.setPreferredSize(new Dimension(175, 25));
		genBillCriteriaPanel.add(cbGenYear, constraints2);
		
		btnGenBill = new JButton(properties.getProperty("label.bill.button.generateBill"));
		btnGenBill.setPreferredSize(new Dimension(175, 25));
		btnGenBill.addActionListener(this);
		genBillCriteriaPanel.add(btnGenBill, constraints3);
		
		// 3. Construct Header Panel
		JPanel headerPanel = new JPanel();
		headerPanel.setOpaque(false);
		headerPanel.setLayout(new GridLayout(0, 1, 10, 10));
		
		JLabel msgLabel = new JLabel(billPanelTitle);
		msgLabel.setFont(new Font("Tahoma", Font.BOLD, 18));
		msgLabel.setHorizontalAlignment(SwingConstants.CENTER);
		headerPanel.add(msgLabel);
		
		ImageIcon imgDivider = new ImageIcon("./images/divider.jpg");
		JLabel lblDivider = new JLabel(imgDivider);
		headerPanel.add(lblDivider);
		
		JPanel genBillPanel = new JPanel();
		genBillPanel.setOpaque(false);
		genBillPanel.setLayout(new BorderLayout());
		genBillPanel.add(headerPanel, BorderLayout.NORTH);
		genBillPanel.add(genBillCriteriaPanel, BorderLayout.CENTER);
		
		return genBillPanel;
	}
}